<script>
var jform = $("#form");
var jsubmit = $("#submit");
var referer = '<?php echo url("friends");?>';
jform.on('submit', function(){
	jform.reset();
	jsubmit.button('loading');
	var postdata = jform.serialize();
	$.xpost(jform.attr('action'), postdata, function(code, message) {
		if(code == 0) {
			$.alert(message);
			jsubmit.text(message).delay(1000).button('reset').location(referer);
		}else if(code == 2) {
			$.alert(message);
		}else if(code == 3) {
            $.alert(message);
        }else if(code == 4) {
            $.alert(message);
        }else if(code == 5) {
            $.alert(message);
        } else {
			$.alert('未知错误请联系管理员！');
			jsubmit.button('reset');
		}
	});
	return false;
});
$('#nav li[fid="friends"]').addClass('active');
</script>
<!--编辑器-->
<script type="text/javascript" src="./plugin/ax_friends/view/wangeditor/dist/js/wangEditor.js"></script>
<script type="text/javascript">
    var editor = new wangEditor('message');
	editor.config.menus = [
        'video',
        'link',
        'emotion',       
    ];
	wangEditor.config.printLog = false;
	editor.config.uploadHeaders = {
        'Accept' : 'text/x-json'
    };
    editor.config.pasteText = true;	
    editor.create();
    <?php if(empty($user)){?>
	editor.disable();
    <?php }?>
</script>
<script src="./plugin/ax_friends/view/js/mip.js"></script>
<script src="./plugin/ax_friends/view/js/mip-showmore.js"></script>
<!--点赞-->
<script type="text/javascript">
$(".z").on('click',function(){
	var Oa=$(this);
	var id=Oa.attr('uid');//获取id属性
	var friendsid=Oa.attr('friendsid');
	var vl=Oa.find("span").text();
	vl=parseInt(vl)+1;
	$.post('<?php echo url("friends-z");?>',{uid:id,friendsid:friendsid},function(data){
		if(data == 0){ 
			$.alert('您已经点过赞了,不要重复赞哦！');
		}else if(data == 1){ 
			Oa.find("span").text(vl);
		}else if(data == 2){ 
            $.alert('请登录后点赞！');
        }else if(data == 3){ 
            $.alert('您所在的用户组禁止操作！');
        }else{
			$.alert('未知错误请联系管理员！');
		}

	},'json'); 
})
</script>
<!--删除-->
<script type="text/javascript">
$(".ax_del").on('click',function(){
	var Oa = $(this);
	var referer = '<?php echo url("friends");?>';
	var friendsid = Oa.attr('friendsid');
	if(!window.confirm("是否删除记录？")) return false;
	$.post('<?php echo url("friends-del");?>',{friendsid:friendsid},function(code){
		if(code == 1){ 
			$.alert('成功删除！').delay(1000).location(referer);
		}else{ 
			$.alert('删除失败！');
		}

	},'json'); 
})
</script>



<script type="text/javascript">
    var jattachparent = $('.attachlist_parent');
    $('#addattach').on('change', function(e) {
    var files = xn.get_files_from_event(e);
    if (files.length > 9) {
            $.alert('最多上传9张图片'); return false;
    }
    if($(".attachlist li").length > 8){
            $.alert('最多上传9张图片'); return false;
    }
    if (!files) return;
    
    // 并发下会 服务端 session 写入会有问题，由客户端控制改为串行
    if (!jattachparent.find('.attachlist').length) {
        jattachparent.append('<fieldset class="fieldset"><legend>上传的图片</legend><ul class="attachlist"><ul></fieldset>');
    }
    
    var jprogress = jattachparent.find('.progress');
    if(!jprogress.length) {
        jprogress = $('<div class="progress"><div class="progress-bar" role="progressbar" style="width: 25%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">25%</div></div>').appendTo(jattachparent);
    }
    jprogressbar = jprogress.find('.progress-bar');
    $.each_sync(files, function(i, callback) {
        var file = files[i];
        xn.upload_file(file, xn.url('attach-create'), {is_image: 0}, function(code, message) {
            if (code != 0) return $.alert(message);
            // 把文件 append 到附件列表
            var url = message.url;
            var filetype = message.filetype;
            var aid = message.aid;
            $('.attachlist').append('<li aid="' + aid + '"><a href="' + message.url + '" target="_blank"><i class="icon filetype ' + filetype + '"></i> ' + message.orgfilename + '</a> <a href="javascript:void(0);" class="delete ml-2"><i class="icon-remove"></i> <?php echo lang('delete');?></a></li>');
            callback();
            jprogress.hide();
        }, function(percent) {
            percent = xn.intval(percent);
            jprogressbar.css('width', percent+'%');
            jprogressbar.text(percent+'%');
            jprogress.show();
            console.log('progress:'+ percent); 
        });
    });
});

// 删除附件
jattachparent.on('click', 'a.delete', function() {
    var jlink = $(this);
    var jli = jlink.parents('li');
    var aid = jli.attr('aid');
    if(!window.confirm(lang.confirm_delete)) return false;
    $.xpost(xn.url('attach-delete-'+aid), function(code, message) {
        if(code != 0) return $.alert(message);
        jlink.parent().remove();
    });
    return false;
})
</script>

